﻿using AutoMapper;
using System.Reflection;

namespace BlogDemo.Application.Configuration
{
    public class AutoMapperConfiguration
    {
        public static IMapper Mapper { get; private set; }

        public static AutoMapper.MapperConfiguration MapConfiguration { get; private set; }

        public static void Configure()
        {
            MapConfiguration = new AutoMapper.MapperConfiguration(cfg =>
            {
               // cfg.ConstructServicesUsing(ObjectFactory.GetInstance);
                // 方法1.加载配置
                //cfg.AddProfile<TokenProfile>();
                //cfg.AddProfile<UserProfile>();

                //方法2.自动加载 Profile
                //var autoMapperProfiles = Assembly.GetExecutingAssembly().GetTypes()
                //.Where(type => !String.IsNullOrEmpty(type.Namespace))
                //.Where(type => type.BaseType != null && type.BaseType == typeof(Profile));       
                //foreach (var type in autoMapperProfiles)
                //{
                //    var profile = (Profile)Activator.CreateInstance(type);
                //    cfg.AddProfile(profile);
                //}
                // 方法2.加载配置
                // var autoMapperProfiles = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.BaseType != null && type.BaseType == typeof(Profile));
                // cfg.AddProfiles(autoMapperProfiles);


                // 方法3.加载Assembly配置
                var myAssembly = Assembly.GetExecutingAssembly();
                cfg.AddMaps(myAssembly);
                // 方法4.加载配置
                //cfg.AddProfiles("Exercise.Services.dll");

                // cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
                // cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
            });
            // only during development, validate your mappings; remove it before release        
            MapConfiguration.AssertConfigurationIsValid();

            Mapper = MapConfiguration.CreateMapper();
        }
    }
}
